package com.topisv.tms.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.EdiOrderItem;
import com.topisv.tms.entity.IdEntity;
import com.topisv.tms.entity.stock.instorage.InstorageCountItem;
import com.topisv.tms.entity.stock.instorage.InstorageUpSubTaskItem;
import com.topisv.tms.entity.stock.instorage.InstorageUpTaskItem;
import com.topisv.tms.entity.work.WorkOrder;
import com.topisv.tms.entity.work.ExceptionOrder.ExceptionBackItem;
import com.topisv.tms.tools.StringCusUtils;

public class EdiOrderUtil {

	public static String getParamName(String param) {
		if ("ordeno".equals(param)) {
			return "订单号";
		} else if ("recena".equals(param)) {
			return "收货人名称";
		} else if ("mobile".equals(param)) {
			return "手机号码";
		} else if ("addres".equals(param)) {
			return "收件地址";
		}
		return null;
	}

	/**
	 * 统计订单的SKU量
	 * @param orderList
	 * @return Map<String,Integer>
	 */
	public static Map<String, Integer> countOrderSku(Collection<WorkOrder> orderList) {
		Map<String, Integer> skuMap = new HashMap<String, Integer>();
		for (WorkOrder o : orderList) {
			Map<String, Integer> maps = countOrderSku(o);
			for (Map.Entry<String, Integer> entry : maps.entrySet()) {
				String sku = entry.getKey();
				Integer num = entry.getValue();
				if (skuMap.containsKey(sku)) {
					skuMap.put(sku, skuMap.get(sku) + num);
				} else {
					skuMap.put(sku, num);
				}
			}
		}
		return skuMap;
	}

	/**
	 * 统计订单的SKU量
	 * @param workOrder
	 * @return Map<String,Integer>
	 */
	public static Map<String, Integer> countOrderSku(WorkOrder workOrder) {
		Map<String, Integer> skuMap = new HashMap<String, Integer>();
		List<EdiOrderItem> eOrderList = workOrder.getOrder().getOrderItems();
		for (EdiOrderItem ediOrderItem : eOrderList) {
			String sku = ediOrderItem.getSku();
			Integer num = ediOrderItem.getQty();
			if (skuMap.containsKey(sku)) {
				skuMap.put(sku, skuMap.get(sku) + num);
			} else {
				skuMap.put(sku, num);
			}
		}
		return skuMap;
	}

	/**
	 * 统计sku数量
	 * @param order
	 * @return int
	 */
	public static int countOrderSkuQty(List<EdiOrder> order) {
		int qty = 0;
		for (EdiOrder ediOrder : order) {
			qty += countOrderSkuQty(ediOrder);
		}
		return qty;
	}

	/**
	 *统计sku数量
	 * @param order
	 * @return int
	 */
	public static int countOrderSkuQty(EdiOrder order) {
		int qty = 0;
		if ((qty = order.getSkuQty()) != 0) {
			return qty;
		}
		List<EdiOrderItem> skulst = order.getSkulst();
		if (skulst == null || skulst.isEmpty()) {
			skulst = order.getOrderItems();
		}
		if (skulst != null) {
			for (EdiOrderItem item : order.getSkulst()) {
				qty += item.getQty();
			}
		}
		return qty;
	}
	
	/**
	 * 统计SKU数量
	 * @param items
	 * @return
	 */
	public static Map<String, Integer> count(List<InstorageCountItem> items){
		Map<String, Integer> skuMap = new HashMap<String, Integer>();
		for (InstorageCountItem ediOrderItem : items) {
			String sku = ediOrderItem.getSku();
			Integer num = ediOrderItem.getNum();
			String errorflag = ediOrderItem.getErrorFlag();
			String key = new StringBuilder().append(sku).append("_").append(errorflag).toString();
			if (skuMap.containsKey(key)) {
				skuMap.put(key, skuMap.get(key) + num);
			} else {
				skuMap.put(key, num);
			}
		}
		return skuMap;
	}
	
	/**
     * 统计SKU数量
     * @param items
     * @return
     */
    public static Map<String, Integer> countNew(List<InstorageCountItem> items){
        Map<String, Integer> skuMap = new HashMap<String, Integer>();
        for (InstorageCountItem item : items) {
            String sku = item.getSku();
            String batchCode = item.getBatchCode() == null ? "" : item.getBatchCode();
            String productDate = item.getProductDate() == null ? "" : item.getProductDate();
            String expireDate = item.getExpireDate() == null ? "" : item.getExpireDate();
            Integer num = item.getNum();
            String errorflag = item.getErrorFlag();
            String key = new StringBuilder().append(sku).append("_").append(batchCode).append("_").append(productDate).
                  append("_").append(expireDate).append("_").append(errorflag).toString();
            if (skuMap.containsKey(key)) {
                skuMap.put(key, skuMap.get(key) + num);
            } else {
                skuMap.put(key, num);
            }
        }
        return skuMap;
    }
	
	/**
	 * 统计SKU数量
	 * @param items
	 * @return
	 */
	public static Map<String, Integer> countByUp(List<InstorageCountItem> items){
		Map<String, Integer> skuMap = new HashMap<String, Integer>();
		for (InstorageCountItem ediOrderItem : items) {
			String sku = ediOrderItem.getSku();
			Integer num = ediOrderItem.getUpNum();
			if (skuMap.containsKey(sku)) {
				skuMap.put(sku, skuMap.get(sku) + num);
			} else {
				skuMap.put(sku, num);
			}
		}
		return skuMap;
	}
	
	public static Map<String, Integer> countUpTaskItem(List<InstorageUpSubTaskItem> items){
		Map<String, Integer> skuMap = new HashMap<String, Integer>();
		for (InstorageUpSubTaskItem item : items) {
			String sku = item.getSkuCode();
			Integer num = item.getNum();
			String errorflag = item.getErrorFlag();
			String key = new StringBuilder().append(sku).append("_").append(errorflag).toString();
			if (skuMap.containsKey(key)) {
				skuMap.put(key, skuMap.get(key) + num);
			} else {
				skuMap.put(key, num);
			}
		}
		return skuMap;
	}
	
	public static Map<String, Integer> countUpTaskItemNew(List<InstorageUpSubTaskItem> items){
        Map<String, Integer> skuMap = new HashMap<String, Integer>();
        for (InstorageUpSubTaskItem item : items) {
            String sku = item.getSkuCode();
            String batchCode = item.getBatchCode() == null ? "" : item.getBatchCode();
            String productDate = item.getProductDate() == null ? "" : item.getProductDate();
            String expireDate = item.getExpireDate() == null ? "" : item.getExpireDate();
            Integer num = item.getNum();
            String errorflag = item.getErrorFlag();
            String key = new StringBuilder().append(sku).append("_").append(batchCode).append("_").append(productDate).
                append("_").append(expireDate).append("_").append(errorflag).toString();
            if (skuMap.containsKey(key)) {
                skuMap.put(key, skuMap.get(key) + num);
            } else {
                skuMap.put(key, num);
            }
        }
        return skuMap;
    }
	
	public static Map<String, Integer> countInstorageUpTaskItem(List<InstorageUpTaskItem> items){
		Map<String, Integer> skuMap = new HashMap<String, Integer>();
		for (InstorageUpTaskItem item : items) {
			String sku = item.getSkuCode();
			Integer num = item.getNum();
			String errorflag = item.getErrorFlag();
			String key = new StringBuilder().append(sku).append("_").append(errorflag).toString();
			if (skuMap.containsKey(key)) {
				skuMap.put(key, skuMap.get(key) + num);
			} else {
				skuMap.put(key, num);
			}
		}
		return skuMap;
	}
	
	public static Map<String, Integer> countInstorageUpTaskItemNew(List<InstorageUpTaskItem> items){
        Map<String, Integer> skuMap = new HashMap<String, Integer>();
        for (InstorageUpTaskItem item : items) {
            String sku = item.getSkuCode();
            String batchCode = StringCusUtils.TransNull(item.getBatchCode());
            String productDate = StringCusUtils.TransNull(item.getProductDate());
            String expireDate = StringCusUtils.TransNull(item.getExpireDate());
            Integer num = item.getNum();
            String errorflag = item.getErrorFlag();
            String key = new StringBuilder().append(sku).append("_").append(batchCode).append("_").append(productDate).append(
                "_").append(expireDate).append("_").append(errorflag).toString();
            if (skuMap.containsKey(key)) {
                skuMap.put(key, skuMap.get(key) + num);
            } else {
                skuMap.put(key, num);
            }
        }
        return skuMap;
    }

	public static Map<String, Integer> countEdiOrderItem(List<EdiOrderItem> items) {
		Map<String, Integer> skuMap = new HashMap<String, Integer>();
		for (EdiOrderItem item : items) {
			String sku = item.getSku();
			Integer num = item.getQty();
			String skus = sku;
			if (skuMap.containsKey(skus)) {
				skuMap.put(sku, skuMap.get(skus) + num);
			} else {
				skuMap.put(sku, num);
			}
		}
		return skuMap;
	}
	public static Map<String, Integer> countExceptionItem(List<ExceptionBackItem> items) {
		Map<String, Integer> skuMap = new HashMap<String, Integer>();
		for (ExceptionBackItem item : items) {
			String sku = item.getSku();
			Integer num = item.getNum();
			String skus = sku;
			if (skuMap.containsKey(skus)) {
				skuMap.put(sku, skuMap.get(skus) + num);
			} else {
				skuMap.put(sku, num);
			}
		}
		return skuMap;
	}
	/**
	 * 判断两个map是否数量一致
	 * @param map1
	 * @param map2
	 * @return boolean
	 */
	public static boolean checkCountNumMatch(Map<String, Integer> map1, Map<String, Integer> map2){
		boolean rs = true;
		for (Map.Entry<String, Integer> entry : map1.entrySet()) {
			String key = entry.getKey();
			Integer preValue = entry.getValue();
			Integer countValue = map2.get(key);
			if (countValue == null || !countValue.equals(preValue)) {
				rs = false;
				break;
			}
		}
		return rs && map1.size() == map2.size();
	}
	/**
	 * 查找ID
	 * @param entities
	 * @return List<Long>
	 */
	public static List<Long> findIds(List<? extends IdEntity> entities) {
		List<Long> rs = new ArrayList<Long>();
		for (IdEntity entity : entities) {
			rs.add(entity.getId());
		}
		return rs;
	}
}
